<?php
abstract class Base_Element 
{
	/**
    * Default values of an element
    */
	protected $defaults  =  array('name'        =>  '',
								  'label'       =>  null,
								  'type'        =>  'Text',
								  'value'       =>  '',
								  'disabled'	=>  false,
								  'readonly'    =>  false
								  );
	
	/**
    * Array of decorators in schema format
    */
	protected $decorators;
	
	/**
    * Array of validators in schema format
    */
	protected $validators = array();
	
	/**
    * Behaviour objects for extra functinaliteit
    */
	protected $behaviours = array();
	
	/**
    * Template wrapper for element
    */
	protected static $_tpl;
	 

	 
	/**
    * Construct
    * ...
    */	
	public function __construct($definition){
		$this->loadTemplate();
		foreach((array) $definition as $name => $value){
			if($name == 'validators'){
				$this->validators[$name] = $value;
			} else {
				$this->defaults[$name] = $value;
			}
		}
	}
	
	
	/**
    * getVariable
    * returns an variable of the defaults array
    */
	public function getVariable($name){
		return $this->defaults[$name];
	}
	
	/**
    * addBehaviour
    * adds an extra behaviour to the elemenet.
    */
	public function addBehaviour($behaviour){
		$this->behaviours[] = $behaviour;
	}
	
	/**
    * getBehaviours
    * returns all behaviours objects
    */
	public function getBehaviours(){
		return $this->behaviours;
	}
	
	/**
    * getValidators
    * Returns a array of validators
    */
	public function getValidators(){
		if(isset($this->validators['validators'])){
			return $this->validators['validators'];
		}
		return array();
	}
	
	/**
    * getDecorators
    * Returns a array of decorators
    */
	public function getDecorators(){
		return $this->decorators;
	}
	
	
	/**
    * loadTemplate
    * sets template for usage
    */
	public function loadTemplate(){
	    if (isset(self::$_tpl)) {
            return;
        }

        self::$_tpl = '%s' . PHP_EOL
					. 'public function get%s()'. PHP_EOL
                    . '{' . PHP_EOL
                    . ' %s' . PHP_EOL . PHP_EOL
					. '	return $%s;'. PHP_EOL
                    . '}' . PHP_EOL. PHP_EOL;
	}
	
	/**
    * buildPhpDocs
    * returns a String with information for an documentor
    */
	protected function buildPhpDocs()
    {
	    $data = '/**'. PHP_EOL .
				'* '. $this->defaults['name'] . PHP_EOL .
				'*/ '	;

        return $data;
    }
	
	/**
    * render
    * returns the rendered final string of the element
    */
	public function render(){
		
		$docs = $this->buildPhpDocs();
		$name = $this->defaults['name'];
		$data = $this->getData();
		
		foreach($this->getBehaviours() as $behaviour){
			$data .= PHP_EOL.$behaviour->execute();
		}
		
		$content = sprintf(self::$_tpl, $docs, $name, $data, $name);
		
		return $content; 
	}
	

}
?>